package com.atguigu.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.respority.HospitalRepository;
import com.atguigu.service.HospitalService;
import com.atguigu.service.HospitalSetService;
import com.atguigu.yygh.cmn.client.DictFeignClient;
import com.atguigu.yygh.enums.DictEnum;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.vo.hosp.HospitalQueryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class HospitalServiceImpl implements HospitalService {
    @Autowired
    private HospitalRepository hospitalRepository;

    @Autowired
    private HospitalSetService hospitalSetService;

    @Override
    public void save(Map<String, Object> paramMap) {

        //1 将要操作的map集合转换为方便操作的对象
        //步骤1 先将map集合转换为json
        String s = JSONObject.toJSONString(paramMap);
        // 步骤2 再将json字符串转换为对象
        Hospital hospital = JSONObject.parseObject(s, Hospital.class);
        //判断这个数据在数据库中是否存在
        String hoscode = hospital.getHoscode();
        Hospital existHospital = hospitalRepository.findByHoscode(hoscode);

        //判断是否存在相同的数据 如果不存在就添加  如果存在就更新
        if (existHospital != null) {
            hospital.setId(existHospital.getId());
            hospital.setCreateTime(existHospital.getCreateTime());
            hospital.setUpdateTime(existHospital.getUpdateTime());
            hospital.setStatus(existHospital.getStatus());
        } else {
            hospital.setUpdateTime(new Date());
            hospital.setCreateTime(new Date());
            hospital.setStatus(1);
        }
        hospitalRepository.save(hospital);
    }

    @Override
    public Hospital getHoscode(String hoscode) {
        return hospitalRepository.findByHoscode(hoscode);
    }

    @Autowired
    private DictFeignClient dictFeignClient; //引用数据字典服务
    @Override
    public Page<Hospital> selectPage(Integer page, Integer limit, HospitalQueryVo hospitalQueryVo) {
        //排序
        Sort sort = Sort.by(Sort.Direction.DESC, "createTime");
        //分页
        PageRequest pageAble = PageRequest.of(page - 1, limit, sort);
        //查询条件
        Hospital hospital = new Hospital();
        BeanUtils.copyProperties(hospitalQueryVo, hospital);
        //3模糊查询
        ExampleMatcher exampleMatcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher
                        .CONTAINING)
                .withIgnoreCase(true);
        //创建 example对象
        Example<Hospital> example = Example.of(hospital, exampleMatcher);
        //调用查询方法
        Page<Hospital> pageResult = hospitalRepository.findAll(example, pageAble);
        //获取当前页的结果集 然后进行遍历  （遍历每一个·医院对象hospital)
        pageResult.getContent().forEach(hosp->{
            this.packHospital(hosp);
        });
        return  pageResult;
    }

    @Override
    public void updateStatus(String id, Integer status) {
        //根据 id和 status去更新
        //1根据id去查询
        Hospital hospital = hospitalRepository.findById(id).get();
        //2、设置status和updateTime
         hospital.setStatus(status);
         hospital.setUpdateTime(new Date());
         //调用save方法
         hospitalRepository.save(hospital);

    }

    @Override
    public Map<String, Object> show(String id) {
        Hospital  hospital = hospitalRepository.findById(id).get();
        //封装hosptital对象
        hospital = this.packHospital(hospital);
        //封装 result返回值
        Map<String,Object> map =new HashMap<>();
        map.put("hospital",hospital);
        map.put("bookingRule",hospital.getBookingRule());
        return map;
    }

    @Override
    public String getHospName(String hoscode) {
        Hospital hospital = hospitalRepository.findByHoscode(hoscode);
        return hospital.getHosname();
    }
   /*
     根据医院名称去查询医院的信息
    */
    @Override
    public List<Hospital> findByHosname(String hosname) {
       return  hospitalRepository.findHospitalByHosnameLike(hosname);
    }

    /**
     *   根据医院编号去查询医院的所有信息
     * @param hoscode
     * @return
     */
    @Override
    public Map<String, Object> item(String hoscode) {
        Map result =new HashMap();
        Hospital hoscode1 = this.getHoscode(hoscode);
        Hospital hospital = this.packHospital(hoscode1);
        result.put("hospital",hospital);
        result.put("bookingRule",hospital.getBookingRule());
        return result;
    }

    private Hospital packHospital(Hospital hosp) {
        String hostype = hosp.getHostype();//医院类型
        String provinceCode = hosp.getProvinceCode();//省份code
        String cityCode = hosp.getCityCode();//城市编号
        String districtCode = hosp.getDistrictCode();//区编号
        //医院服务调用数据字典服务 根据value(dictCode)查询数据字典的名称
        String hostypeString = dictFeignClient.getName(DictEnum.HOSTYPE.getDictCode(),hostype);
        String provinceString = dictFeignClient.getName(provinceCode);
        String cityString = dictFeignClient.getName(cityCode);
        String districtString = dictFeignClient.getName(districtCode);
        String fullAddress = provinceString + cityString + districtString + hosp.getAddress();
        hosp.getParam().put("hostypeString",hostypeString);
        hosp.getParam().put("fullAddress",fullAddress);
        return  hosp;
    }

}
